package cn.zucc.edu.handyzucc.service.impl;

import cn.zucc.edu.handyzucc.dao.TeacherDao;
import cn.zucc.edu.handyzucc.entity.Teacher;
import cn.zucc.edu.handyzucc.service.TeacherService;
import cn.zucc.edu.handyzucc.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherDao teacherDao;

    @Override
    public boolean signup(Teacher teacher) {
        return false;
    }

    @Override
    public List<Teacher> getAllTeachers() {
        return teacherDao.getAllTeachers();
    }

    @Override
    public List<Teacher> searchTeachers(String keyword) {
        return teacherDao.searchTeachers(keyword);
    }

    @Override
    public Teacher getTeacherDetail(Integer teacher_id) {
        return teacherDao.getTeacherDetail(teacher_id);
    }

    @Override
    public boolean deleteTeacher(Integer teacher_id) {
        return teacherDao.deleteTeacher(teacher_id) == 1;
    }

    @Override
    public boolean updateTeacher(Teacher teacher) {
        return teacherDao.updateTeacher(teacher) == 1;
    }

    @Override
    public List<Teacher> batchSignup(MultipartFile file) {
        List<Teacher> teachers = new ArrayList<>();
        try {
            InputStream inputStream = file.getInputStream();
            List<List<Object>> list = ExcelUtils.getStudentListByExcel(inputStream, file.getOriginalFilename());
            inputStream.close();

            for (int i = 0; i < list.size(); i++) {
                List<Object> teacherList = list.get(i);

                Teacher teacher = new Teacher();
                teacher.setTeacher_name(teacherList.get(0).toString());
                teacher.setTeacher_office(teacherList.get(1).toString());
                teacher.setTeacher_phone(teacherList.get(2).toString());
                teacher.setTeacher_email(teacherList.get(3).toString());
                teacher.setTeacher_wechat(teacherList.get(4).toString());
                teacher.setTeacher_qq(teacherList.get(5).toString());

                teacherDao.signup(teacher);

                teachers.add(teacher);
            }
        } catch (Exception e) {

        }
        return teachers;
    }
}
